import 'package:client_ui/api/api/ConnectApi.dart';
import 'package:client_ui/util/GlobalData.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../api/Request.dart';
import '../component/UserMessageComponent.dart';
import '../util/GenerateUtil.dart';

class UserChatPage extends StatefulWidget {
  const UserChatPage({super.key});

  @override
  State<UserChatPage> createState() => _UserChatPageState();
}

class _UserChatPageState extends State<UserChatPage> {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    String title = Provider.of<GlobalData>(context).name;

    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 24,
        title: Padding(
          padding: const EdgeInsets.symmetric(
            vertical: 12.0,
          ),
          child: Text(
            title,
            style: const TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
              color: Colors.black26,
            ),
          ),
        ),
      ),
      body: Container(
        margin: EdgeInsets.all(10),
        // padding: EdgeInsets.all(10),
        color: Colors.black26,
        child: Center(
          child: Column(
            children: [
              // Flexible(flex: 2, child: Column(children: chatDisplayList)),
              Flexible(
                  flex: 2,
                  child: Consumer<GlobalData>(
                    builder: (BuildContext context, GlobalData value,
                        Widget? child) {
                      return ListView.builder(
                        itemCount: value.chatDisplayList.length,
                        itemBuilder: (context, index) {
                          return value.chatDisplayList[index];
                        },
                      );
                    },
                  )),
              Flexible(flex: 1, child: generateSendBox()),
            ],
          ),
        ),
      ),
    );
  }

  Widget generateSendBox() {
    return Scaffold(
        appBar: AppBar(
          toolbarHeight: 40,
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.settings),
              onPressed: () {},
            ),
            IconButton(
              icon: Icon(Icons.send),
              onPressed: () {
                if (_controller.text == '') {
                  GenerateUtil.showTimedSnackBar(context, '请输入聊天内容');
                  return;
                }

                ConnectApi.sendChatMsg(
                    1,
                    'https://picx.zhimg.com/v2-c87d34b747c2d81f7cd09fd87dfee2a1_1440w.jpg',
                    _controller.text);
                _controller.clear();
              },
            ),
          ],
        ),
        body: Container(
          // width: 200,
          height: 100,
          decoration: BoxDecoration(
            border: Border.all(
              color: Colors.grey,
              width: 1,
            ),
          ),
          child: TextField(
            controller: _controller,
            maxLines: null,
            decoration: const InputDecoration(
              border: OutlineInputBorder(
                borderSide: BorderSide.none,
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide.none,
              ),
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide.none,
              ),
            ),
          ),
        ));
  }
}
